Advanced iOS App Architecture

By René Cacheaux & Josh Berlin Copyright ©2020 Razeware LLC.

Notice of Rights

All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner.


Notice of Liability

This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software.


Trademarks

All trademarks and registered trademarks appearing in this book are the property of their own respective owners.


Table of Contents: Overview

About the Cover 7

What You Need 11

Book License 13

Book Source Code & Forums 14

Chapter 1: Welcome 16

Chapter 2: Which Architecture Is Right for Me? 18

Chapter 3: Example App: Koober 37

Chapter 4: Objects & Their Dependencies 52

Chapter 5: Architecture: MVVM 124

Chapter 6: Architecture: Redux 181

Chapter 7: Architecture: Elements, Part 1 235

Chapter 8: Architecture: Elements, Part 2 257

Chapter 9: Getting Ready for SwiftUI 327

Conclusion 338

Table of Contents: Extended

About the Cover 7

About the Authors 9

About the Editors 9

What You Need 11

Book License 13

Book Source Code & Forums 14

Chapter 1: Welcome 16

What lies ahead 16

Who this book is for 17

Where to go from here? 17

Chapter 2: Which Architecture Is Right for Me? 18

Identifying problems to solve 19

Boosting team velocity and strengthening code quality 19

Examining the problems 21

Increasing code agility 29

Surveying architecture patterns 31

Selecting a pattern 33

Putting patterns into practice 34

Key points 36

Chapter 3: Example App: Koober 37

Koober 37

Why Koober? 45

Getting started with the source 46

Key points 51

Chapter 4: Objects & Their Dependencies 52

Establishing the goals 52

Learning the lingo 53

Creating dependencies 56

The fundamental considerations 57

Why is this architecture? 58

Dependency patterns 59

Dependency Injection 60

On-demand approach 64

Factories approach 66

Single-container approach 72

Designing container hierarchies 75

Applying DI theory to iOS apps 80

Applying the on-demand approach 88

Applying the factories approach 94

Applying the single-container approach 107

Applying the container hierarchy approach 115

Key points 122

Where to go from here? 122

Chapter 5: Architecture: MVVM 124

What is it? 125

Container views 131

Communicating amongst view models 133

Navigating 134

Applying theory to iOS apps 137

Composing views 148

Navigating 158

Managing state 172

Key points 178

Pros and cons of MVVM 178

Where to go from here? 180

Chapter 6: Architecture: Redux 181

What is Redux? 182

Applying theory to iOS apps 194

Key points 232

Pros and cons of Redux 232

Where to go from here? 234

Chapter 7: Architecture: Elements, Part 1 235

Introducing Elements 237

Underlying concepts of Elements 238

User interface 241

Interaction responder 249

Key points 256

Chapter 8: Architecture: Elements, Part 2 257

Observer 257

Use case 293

Pros and cons of Elements 325

Key points 326

Chapter 9: Getting Ready for SwiftUI 327

What SwiftUI means for app architecture 328

Architecting with SwiftUI versus UIKit 329

When to start building with SwiftUI 330

Practicing 330

Preparing 332

Planning 333

Where to go from here? 336

Key points 337

Conclusion 338

AAbout the Cover

Although pufferfish look like the cutest little squeezable stress balls in the sea, they’re nothing you would ever want to cuddle up next to. Most pufferfish contain toxins to protect themselves from predators; one pufferfish alone can contain enough toxin to kill 30 humans — with no known antidote. James Bond knows this all too well.

But we didn’t feature the pufferfish because learning about advanced architecture is toxic; rather, pufferfish are one of nature’s great architects. During spawning season, using nothing but their fins, male pufferfish create stunning geometric patterns in the sand on the seabed as potential nesting sites for females.

These structures can be up to seven feet in diameter and take the males anywhere from seven to nine days to build. It’s not certain if the structure serves to attract the females, or if the females are simply drawn to the soft sand the male stirred up in the process.

So when you’re getting frustrated with Xcode or Swift, just be thankful that you have better tools to architect with than a pair of fins!

For an extra-cool view on how the male pufferfish architects his “software,” check out this video: https://www.youtube.com/watch?v=p1PID91sEW8.



Dedications

"To my beautiful wife Lauren, to my fun-loving angel Zara, to my soon-to-arrive son René Jr., to my parents who have given me everything, and, last but not least, to my furry pals Paco and Charlie. I love you all."

René Cacheaux


"Thanks to my parents for buying me my first, second, and third computers, and making me put them together myself. Thanks for allowing me to take any path I wanted in life, even when it’s a little crazy. Love y'all."

Josh Berlin


About the Authors

Josh Berlin is an author of this book. He loves building thoughtful user experiences on mobile. He’s currently an iOS engineer at Hatch building products to help people sleep better. He's built apps for the iPhone and iPad since 2008. Josh recently finished culinary school in Austin, TX. When he's not coding, he's probably cooking or dreaming of food.


René Cacheaux is an author of this book. He loves to architect and build software. He currently is a Mobile Architect at Atlassian where his mission is to design Atlassian's mobile platform. He especially loves all things mobile and currently architects for both Android and Apple platforms. René has been engineering iOS apps since 2009 and has experience in mobile client and server engineering, mobile user experience design and product management. René has worked on a wide range of apps spanning from industrial sales enablement to world-wide social networking. René enjoys starting his days in true Austin-Texas fashion with a a breakfast taco alongside a freshly brewed cappuccino. In addition to building mobile apps, he loves to travel, snow ski, ocean kayak and root for his alma mater, the Texas Longhorns.


About the Editors

Darren Ferguson is doubling as the tech editor and the final pass editor for this book. He's an experienced software developer and works for M.C. Dean, Inc, a systems integration provider from North Virginia. When he's not coding, you'll find him enjoying EPL Football, traveling as much as possible and spending time with his wife and daughter. Find Darren on Twitter at @darren102.


About the Artist

Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate.

WWhat You Need

To follow along with this book, you'll need the following:

CConclusion

What a journey it’s been! From exploring why architecture matters to diving deep into dependency injection, to comparing different architecture patterns, you’ve gotten a taste of architecting iOS apps using advanced techniques and patterns.

By putting the book concepts to practice, your codebase will become easier to work in and you’ll have more fun writing code. Not only that, you’ll be able to respond quickly to changing requirements.

We hope the ideas you saw in this book inspire you to explore and try out different architecture practices and maybe even inspire you to come up with some of your own!

If you’re hungry for more architecture related books we recommend reading Design Patterns by Tutorials. Also, if you found some of the Combine code hard to follow, we recommend checking out Combine Asynchronous Programming with Swift.

If you have any questions or comments as you work through this book, please stop by our forums at http://forums.raywenderlich.com and look for the particular forum category for this book.

Thank you again for purchasing this book. Your continued support is what makes the books, tutorials, videos and other things we do at raywenderlich.com possible. We truly appreciate it!

Happy architecting!

– The Advanced iOS App Architecture team